<?php

namespace app\api\model;

use think\Model;
use think\Db;
class Orders extends Model
{
    private static $instance = null;
    private $company = '';

	public function __construct($data = [])
    {
        parent::__construct($data);
        $this->company = config('companys.list');
    }

    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            $obj = new static();
            self::$instance = $obj;
            return $obj;
        } else {
            return self::$instance;
        }
    }

    /**
     * 插入订单信息
     */
    public function insertOrder($addData)
    {
        Db::name('order')->insert($addData);
        return Db::name('order')->getLastInsID();
    }

    public function getOrderInfo($where = [])
    {
        $orderInfo = Db::name('order')->alias('o')
            ->join('t_product p', 'o.pro_id = p.id', 'left')
            ->join('t_order_applicant oa', 'o.num_id = oa.num_id', 'left')
            ->where($where)
            ->field('p.pro_num, oa.credit_code, o.num_id, o.pro_id, p.company_id, o.applicant_name, o.policy_files, o.vcodeMobile')
            ->find();
        return $orderInfo;
    }

    /**
     * 获取批改列表信息
     */
    public function getChangeList($where)
    {
        $list = Db::name('policy_info_change')->alias('pi')
            ->join('t_order o', 'pi.num_id = o.num_id', 'left')
            ->join('t_product p', 'o.pro_id = p.id', 'left')
            ->where($where)
            ->field('pi.id AS changeId, pi.num_id, pi.check_inc, pi.check_dec, pi.check_num, pi.check_status, pi.s_time, pi.e_time, pi.c_time, pi.money, o.policy_num, o.applicant_name,pi.electronic_file,p.name AS pro_name, p.company_id, p.id AS pId, pi.pay_url AS payUrl, pi.pay_type AS payType')
            ->select();

        foreach ($list as $k => $v)
        {
            $list[$k]['is_effect'] = (empty($v['check_num']) || empty($v['electronic_file'])) ? '待生效' : '已生效';
            $list[$k]['company_name'] = '中国平安';//$this->company[$v['company_id']];
            $list[$k]['is_add'] = $v['check_inc'] > $v['check_dec'] ? '增加人员' : '替换人员';

            $list[$k]['pay_type'] = empty($v['payUrl']) ? ($v['payType'] == 1 ? 1 : 4) : 0;//0 线上   1  线下   4 两种方式都支持

            $pro_Other = Db::name('product_other')->where(['pro_id' => $v['pId']])->field('company_name,company_bank_no,company_bank_name,company_username')->find();
            $list[$k]['other'] = $pro_Other;
        }

        return $list;
    }

    /**
     * 获取批改详情
     */
    public function getChangeDetail($where)
    {
        $list = Db::name('employee_info_record')->where($where)->field('name, paper_num, change_id')->select();
        return $list;
    }

    /**
     * 插入投保流程留存表
     */
    public function setRetention($data)
    {
        Db::name('order_retention')->insert($data);
        return Db::name('order_retention')->getLastInsID();
    }


































	//获取用户订单列表
	public  static function  get_order_list($params){	
	 $list=Db::name('order')->alias('o')
                ->join('t_product p','o.pro_id=p.id','left')
				->join('t_channel c','o.channel_id=c.id','left')
				->join('t_career_type ct','o.career_type=ct.id','left')
                ->where(['o.uid'=>['eq',$params['uid']]])
                ->field('o.num_id,o.policy_num,o.applicant_name,o.policy_money,o.status,p.name,p.company_id,c.name AS channel_name,ct.name AS ct_name')
                ->select();
     foreach($list as $k=>$v){
		  $list[$k]['company_name'] = '中国平安';//config('companys.list')[$v['company_id']];
	 }				
    return	$list;			
	}
	//获取用户订单详情 @author zcs 
	public static function  get_order_detail($params){
		$list=Db::name('order')->alias('o')
		          ->join('t_order_applicant a','o.num_id=a.num_id','left')
				  ->join('t_product p','o.pro_id=p.id','left')
				  ->where(['o.num_id'=>['eq',$params['num_id']]])
				  ->field('o.s_time,o.e_time,o.c_time,p.name,a.com_name,a.credit_code,a.contact_name,a.contact_tel,a.contact_addr')
				  ->select();
		
		return $list;
	}
	//获取订单在保人员 @author zcs 
	public static function get_insured($params){
		$list=Db::name('employee_info_record')
		     ->where(['num_id'=>['eq',$params['num_id']],'is_insured'=>['eq',1]])
			 ->field('name,paper_num,age,s_time,e_time')
			 ->select();
		return $list;
	}
	
	//获取批改信息列表 @author zcs 
	public static function get_correcting($params){
		 $where=[
		'o.num_id'=>$params['num_id']
	   ];
	   $list = Db::table('t_order')->alias('o')
                ->join('t_policy_info_change oc', 'o.num_id = oc.num_id', 'left')
                ->join('t_product p', 'o.pro_id = p.id', 'left')
				->where($where)
				->field('oc.id,o.num_id,o.policy_num,o.applicant_name,p.name')
				->select();
		return $list;
		
	}
	//获取批改信息详情 @author zcs 
	public static function get_correcting_detail($params){
		
		$where=[
		   'o.num_id'=>$params['num_id']
			  ];
		$list = Db::table('t_order')->alias('o')
		        ->join('t_policy_info_change oc','o.num_id=oc.num_id','left')
				->join('t_employee_info_record c','o.num_id=c.num_id','left')
				->join('t_product_program pp','o.program_id=pp.id','left')
				->where($where)
				->field('o.num_id,c.job_name,pp.safeguard')
				->find();
		
			$list['safeguard']=json_decode($list['safeguard'],true);
		return $list;
	}
	//获取用户理赔信息 @author zcs 
	public  static function  uid_claim_order($params){
	 $list=	Db::name('claim_order')->where(['uid'=>['eq',$params['uid']]])->field('claim_sn,pro_name,assured,happen_address,happen_time')->select();
		
		return $list;
		
	}
	
	//修改批单
	public static function edit_correcting(){
		
	}
	
	
	//获取产品信息详情 @author zcs 
	public static function get_product($params){
		 
		$list= Db::table('t_product')->alias('p')
		     ->join('t_program pg','p.id=pg.pro_id','left')
			 ->join('t_product_program pp','pg.id=pp.program_id','left')
			 ->join('t_product_other po','p.id=po.pro_id','left')
		     ->where(['p.id'=>['eq',$params['id']]])
			 ->field('p.id,p.name,p.company_id,p.instructions,p.list_img,p.detail_img,pg.name AS pgname,pp.year_money,pp.month_money')
			 ->find();
		$program=Db::table('t_program')->alias('p')
		     ->join('t_product_program pp','p.id=pp.program_id','left')
			 ->where(['p.pro_id'=>['eq',$params['id']]])
			 ->field('p.name,pp.safeguard,pp.year_money,pp.month_money')
			 ->group('p.name')
			 ->select();
			 foreach($program as $k=>$v){
				$program[$k]['safeguard']=json_decode($v['safeguard'],true); 
			 }
		$list['company_name'] = '中国平安';//config('companys.list')[$list['company_id']];
		$list['program']=$program;
		return $list;
	}
	
	
	


}